﻿using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using ZhiJiao.Dal;
using ZhiJiao.Models.Entities;
using ZhiJiao.Models.Entities.Enums;
using ZhiJiao.Service.Abstracts;

namespace ZhiJiao.Service.Services
{
    public class CurrentUser:ICurrentUser
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly ZJDbContext _db;
        public CurrentUser(IHttpContextAccessor httpContextAccessor,
                           ZJDbContext zJDbContext) {
            _httpContextAccessor= httpContextAccessor;
            _db= zJDbContext;
        }

        // 用户ID
        public User User 
        { get 
            {
                var id = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                var user = _db.Users.Include(u=>u.Instruction).FirstOrDefault(u=>u.Id.Equals(id));
                return user;
            }
        }
    }
}
